/* 
 * AP(r) Computer Science GridWorld Case Study:
 * Copyright(c) 2005-2006 Cay S. Horstmann (http://horstmann.com)
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * @author Chris Nevison
 * @author Barbara Cloud Wells
 * @author Cay Horstmann
 */
import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.grid.Location;

import java.util.ArrayList;

public class KingCrab extends CrabCritter {
	public KingCrab() {

	}

	public void processActors(ArrayList<Actor> actors) {
		for (Actor a : actors) {
			if (!isVaild_PushAway(a)) {
				a.removeSelfFromGrid();
			}
		}
	}

	// 判断 actor 移出有效的位置，没有则删除
	private boolean isVaild_PushAway(Actor actor) {
		Grid grid = getGrid();
		// 获取当前 actor 的 邻近有效空位置
		ArrayList<Location> locs = grid.getEmptyAdjacentLocations(actor.getLocation());
		for (Location loc : locs) {
			Location KC_loc = getLocation();
			int d_r = KC_loc.getRow() - loc.getRow();
			int d_c = KC_loc.getCol() - loc.getCol();
			if (Math.abs(d_r - d_c) >= 1) {
				actor.moveTo(loc);
				return true;
			}
		}
		return false;
	}

}
